package org.dfzt.modules.online.config.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;

import org.dfzt.modules.online.config.service.a.MysqlHandler;
import org.dfzt.modules.online.config.service.a.PostgresqlHander;
import org.dfzt.modules.online.config.service.a.SqlserverHander;
import org.hibernate.Session;
import org.dfzt.common.util.SpringContextUtils;
import org.dfzt.common.util.oConvertUtils;
import org.dfzt.modules.online.config.exception.DBException;
import org.dfzt.modules.online.config.service.DbTableHandleI;
import org.dfzt.modules.online.config.service.a.OracleHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class d {
    private static final Logger log = LoggerFactory.getLogger(d.class);

    public static String a = "";


    public static DbTableHandleI getTableHandle() throws SQLException, DBException {
        String str = getDatabaseType();
        switch (str) {
            case "MYSQL":
                return new MysqlHandler();
            case "ORACLE":
                return new OracleHandler();
            case "SQLSERVER":
                return new SqlserverHander();
            case "POSTGRESQL":
                return new PostgresqlHander();
        }

        return null;
    }


    public static Connection getConnection() throws SQLException {
        DataSource dataSource = (DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class);
        return dataSource.getConnection();
    }


    public static String getDatabaseType() throws SQLException, DBException {
        if (oConvertUtils.isNotEmpty(a)) {
            return a;
        }
        DataSource dataSource = (DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class);
        return a(dataSource);
    }


    public static boolean a() {
        try {
            return "ORACLE".equals(getDatabaseType());
        } catch (SQLException sQLException) {
            sQLException.printStackTrace();
        } catch (DBException dBException) {
            dBException.printStackTrace();
        }
        return false;
    }


    public static String a(DataSource paramDataSource) throws SQLException, DBException {
        if ("".equals(a)) {
          Connection connection = paramDataSource.getConnection();
            try {
                DatabaseMetaData databaseMetaData = connection.getMetaData();
                String str = databaseMetaData.getDatabaseProductName().toLowerCase();
                if (str.indexOf("mysql") >= 0) {
                    a = "MYSQL";
                } else if (str.indexOf("oracle") >= 0) {
                    a = "ORACLE";
                } else if (str.indexOf("sqlserver") >= 0 || str.indexOf("sql server") >= 0) {
                    a = "SQLSERVER";
                } else if (str.indexOf("postgresql") >= 0) {
                    a = "POSTGRESQL";
                } else {
                    throw new DBException("数据库类型:[" + str + "]不识别!");
                }
            } catch (Exception exception) {
                log.error(exception.getMessage(), exception);
            } finally {
                connection.close();
            }
        }
        return a;
    }


    public static String a(Connection paramConnection) throws SQLException, DBException {
        if ("".equals(a)) {
            DatabaseMetaData databaseMetaData = paramConnection.getMetaData();
            String str = databaseMetaData.getDatabaseProductName().toLowerCase();
            if (str.indexOf("mysql") >= 0) {
                a = "MYSQL";
            } else if (str.indexOf("oracle") >= 0) {
                a = "ORACLE";
            } else if (str.indexOf("sqlserver") >= 0 || str.indexOf("sql server") >= 0) {
                a = "SQLSERVER";
            } else if (str.indexOf("postgresql") >= 0) {
                a = "POSTGRESQL";
            } else {
                throw new DBException("数据库类型:[" + str + "]不识别!");
            }
        }
        return a;
    }


    public static String a(Session paramSession) throws SQLException, DBException {
        return getDatabaseType();
    }


    public static String a(String paramString1, String paramString2) {
        switch (paramString2) {
            case "ORACLE":
                return paramString1.toUpperCase();
            case "POSTGRESQL":
                return paramString1.toLowerCase();
        }
        return paramString1;
    }


    public static Boolean a(String paramString) {
      Connection connection = null;
      ResultSet resultSet = null;
        try {
            String[] arrayOfString = {"TABLE"};
            connection = getConnection();
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            String str1 = databaseMetaData.getDatabaseProductName().toUpperCase();
            String str2 = a(paramString, str1);
            resultSet = databaseMetaData.getTables(null, null, str2, arrayOfString);
            if (resultSet.next()) {
                log.info("数据库表：【" + paramString + "】已存在");
                return Boolean.valueOf(true);
            }
            return Boolean.valueOf(false);
        } catch (SQLException sQLException) {
            throw new RuntimeException();
        } finally {
            try {
                if (resultSet != null) {
                  resultSet.close();
                }
                if (connection != null) {
                  connection.close();
                }
            } catch (SQLException sQLException) {
                log.error(sQLException.getMessage(), sQLException);
            }
        }
    }


    public static Map<String, Object> a(List<Map<String, Object>> paramList) {
        HashMap hashMap = new HashMap();
        for (byte b1 = 0; b1 < paramList.size(); b1++) {
            hashMap.put(((Map) paramList.get(b1)).get("column_name").toString(), paramList.get(b1));
        }
        return hashMap;
    }


    public static String getDialect() throws SQLException, DBException {
        String str = getDatabaseType();
        return b(str);
    }


    public static String b(String paramString) throws SQLException, DBException {
        String str = "org.hibernate.dialect.MySQL5InnoDBDialect";
        switch (paramString) {
            case "SQLSERVER":
                str = "org.hibernate.dialect.SQLServerDialect";
                break;
            case "POSTGRESQL":
                str = "org.hibernate.dialect.PostgreSQLDialect";
                break;
            case "ORACLE":
                str = "org.hibernate.dialect.OracleDialect";
                break;
        }


        return str;
    }


    public static String c(String paramString) throws SQLException, DBException {
        return paramString;
    }
}
